<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>红绿灯模拟</title>
  <style>
    .light {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      margin: 10px;
      display: inline-block;
      border: 2px solid black;
    }
    .red { background-color: red; }
    .green { background-color: green; }
    .yellow { background-color: yellow; }
    .off { background-color: gray; }
  </style>
</head>
<body>
  <div class="light red"></div>
  <div class="light off"></div>
  <div class="light off"></div>

  <script>
    const lights = document.querySelectorAll('.light');
    let currentLightIndex = 0;
    const lightColors = ['red', 'green', 'yellow'];

    function changeLight() {
      // 将所有灯设置为关闭状态
      lights.forEach((light, index) => {
        light.classList.remove(lightColors[index]);
        light.classList.add('off');
      });

      // 点亮当前灯
      lights[currentLightIndex].classList.remove('off');
      lights[currentLightIndex].classList.add(lightColors[currentLightIndex]);

      // 更新当前灯的索引
      currentLightIndex = (currentLightIndex + 1) % lightColors.length;
    }

    // 每秒切换一次灯
    setInterval(changeLight, 1000);
  </script>
</body>
</html>
